package progetto;

import example.hot.InetCoordinates;
import peersim.config.Configuration;
import peersim.core.IdleProtocol;
import peersim.core.Node;
import peersim.core.Protocol;

public class BluetoothTransm {

	private static BluetoothTransm singleObject;
	
	
	/**
	 * The Bluetooth transmission radius parameter. 
	 * It must has a value in the interval [10, 30] m
	 * Default value: 20m.
	 * 
	 * @config
	 */
	//private static final String PAR_RADIUS = "radius";

	/**
	 * Default value of Bluetooth transmission radius
	 */
	public static final int DEFAULT_RADIUS = 20;
	/**
	 * Max value of Bluetooth transmission radius
	 */
	private static final int MAX_RADIUS = 30;
	/**
	 * Min value of Bluetooth transmission radius
	 */
	private static final int MIN_RADIUS = 10;


	/**
	 * Bluetooth transmission radius.
	 * Obtained from the normalization of radiusPercent (config property)
	 * 
	 */
	private double radiusPercent;

	/**
	 * Bluetooth transmission radius percent.
	 * 
	 */
	private double radius;
	
	private BluetoothTransm() {
		
	     radius = -1;
	     radiusPercent = -1;
	     
	     
	}
	
	public void initializesRadius(double radiusPercent) {
		
		if(this.radiusPercent == -1) 
			if(radiusPercent < MIN_RADIUS || radiusPercent > MAX_RADIUS) {
				this.radiusPercent = DEFAULT_RADIUS;
				System.err.println("Value of Bluetooth transmission radius not valid, used the default value: "+ DEFAULT_RADIUS+"m");
			} else  {
				this.radiusPercent = radiusPercent;
	    	 
			}
	     
		if(this.radius == -1)
			this.radius = getNormalizedValue();
		
	}
	
	
	 // radiusPercent : sizeSquare = x : 1
    private double getNormalizedValue() {
    	double size = SquareSize.getIstance().getSize();
    	if(size == -1) 
    		throw new RuntimeException("Size not initialized!!");
    	
    	System.out.println("radius "+ radiusPercent / size);
    	return radiusPercent / size;
    	
    }
	
	public static BluetoothTransm getInstance() {
		if(singleObject == null) 
			singleObject = new BluetoothTransm();
		
		return singleObject;
	}

	public double getRadius() {
		return radius;
	}


	private void setRadius(double radius) {
		this.radius = radius;
	}

	/**
     * Calculates the Euclidean distance of two nodes
     * 
     * @param current
     * 				the node current
     * @param candidate
     * 				the other node 
     * @param coordPid
     * 				coordinate protocol identifier
     * @return the Euclidean distance of the two nodes
     */
	private double getDistance(Node node, Node current, int coordPid) {
		// TODO Auto-generated method stub
		
		
		double x1 = ((InetCoordinates) node.getProtocol(coordPid)).getX();
		double x2 = ((InetCoordinates) current.getProtocol(coordPid)).getX();
		double y1 = ((InetCoordinates) node.getProtocol(coordPid)).getY();
		double y2 = ((InetCoordinates) current.getProtocol(coordPid)).getY();
		if (x1 == -1 || x2 == -1 || y1 == -1 || y2 == -1)
            throw new RuntimeException("Found un-initialized coordinate. Use e.g., InetInitializer class in the config file.");
		
		return EuclideanDistance.getDistance(x1, x2, y1, y2);
		//return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));*/
		
		
	}
	
	
	public boolean inTheTrasmRadius(Node node, Node current, int coordPid) {
		if(radius == -1)
			throw new RuntimeException("Radius not initialized!");
		
		return getDistance(node, current, coordPid) <= radius;
	}
	
}

